package academic.ui.client.presenter;


import java.util.ArrayList;

import academic.ui.client.CheckoutServiceAsync;
import academic.ui.client.CustomerServiceAsync;
import ecommerce.soco.shared.GWT_CreditCard;
import ecommerce.soco.shared.GWT_Customer;
import ecommerce.soco.shared.GWT_Zone;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class MyProfilePresenter extends PresenterWidget<MyProfilePresenter.MyView>
{

	private final CustomerServiceAsync service;
	
	private final PlaceManager placeManager;
	
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> slot = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public MyProfilePresenter(EventBus eventBus, MyView view,CustomerServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view);
		this.placeManager=placeManager;
		this.service = service;
	}

	
	public interface MyView extends View {

		void showProfileTab(GWT_Customer result);

		HasClickHandlers updateLocation();

		boolean checkLocationRules();

		GWT_Customer getCustomerUpdateAddress();

		HasClickHandlers updatePersonal();

		boolean checkPersonalRules();

		GWT_Customer getCustomerUpdatePersonal();

		void emailAlreadyExists();

	}
	
	@Override
	public void onReveal() {
		Log.debug("MyProfilePresenter onReveal");
		service.getCurrentCustomer(new AsyncCallback<GWT_Customer>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GWT_Customer result) {
				getView().showProfileTab(result);
				
			}	
		});
		
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("MyProfilePresenter onBind");
		registerHandler(getView().updateLocation().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(!getView().checkLocationRules())
					Log.debug("invalid location data");
				else
					service.updateLocation(getView().getCustomerUpdateAddress(),new AsyncCallback<Boolean>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Boolean result) {
							
						}
						
					});
			}
			
		})); 
		
		registerHandler(getView().updatePersonal().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(!getView().checkPersonalRules())
					Log.debug("invalid personal data");
				else
					service.updatePersonalInfo(getView().getCustomerUpdatePersonal(), new AsyncCallback<Boolean>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Boolean result) {
							if(!result)
							{//an account already exists for the email address
								getView().emailAlreadyExists();
							}
						}
						
					});
			}
			
		}));
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
	}
	
	@Override
	public void onReset() {
		super.onReset();
		Log.debug("MyProfilePresenter onReset");
	}

}
